<?php

namespace app\api\service\jingdong;

use app\lib\jingdong\Jing;
use app\lib\jingdong\jingdongSdk\jd\JdClient;
use app\lib\jingdong\jingdongSdk\jd\request\CtpWareSkuGetBrotherListRequest;
use app\lib\jingdong\jingdongSdk\jd\request\CtpWareSkuGetSkuDetailRequest;
use app\lib\jingdong\jingdongSdk\jd\request\CtpWareSkuGetSkuListRequest;
use app\lib\jingdong\jingdongSdk\jd\request\CtpWareStockQueryAreaStockStateRequest;
use http\Exception\InvalidArgumentException;

class JingDongGoodsService extends Jing
{

    /**
     * @var JingDongGoodsService
     */
    private static $instance;

    public static function getInstance(): JingDongGoodsService
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 获取开普勒系统商品列表
     */
    public function getSkuList()
    {
        $c = new JdClient();

        $c->accessToken = $this->access_token();
        $req = new CtpWareSkuGetSkuListRequest();
        $ctpProtocol    =   array(
            "customerId"=>$c->customerId,
            "channelId"=>$c->channelId,
            "opName"=>$c->opName,
            "appKey"=>$c->appKey
        );
        $apiSkuListParam    =   array(
            "pageSize"=>100
        );

        $req->setApiSkuListParam($apiSkuListParam);
        $req->setCtpProtocol($ctpProtocol);

        $resp = $c->execute($req, $c->accessToken);

        return $resp;
    }

    /**
     * 商品详情
     * */
    public function getSkuDetail($skuId)
    {
        $c = new JdClient();

        $c->accessToken = $this->access_token();
        $req = new CtpWareSkuGetSkuDetailRequest();
        $ctpProtocol    =   array(
            "customerId"=>$c->customerId,
            "channelId"=>$c->channelId,
            "opName"=>$c->opName,
            "appKey"=>$c->appKey
        );
        $skuDetailParam    =   array(
            "skuIdSet"=>$skuId,
            "detailAssemblyType"=>0
        );

        $req->setCtpProtocol($ctpProtocol);
        $req->setSkuDetailParam($skuDetailParam);

        $resp = $c->execute($req, $c->accessToken);

        return $resp;
    }

    /**
     * 获取库存状态
     * */
    public function queryAreaStockState($attrs  =   [])
    {
        $c = new JdClient();

        $c->accessToken = $this->access_token();
        $req    =   new CtpWareStockQueryAreaStockStateRequest();

        $stockStateParam    =   array(
            "skuQuantityList"=>array([
                //商品数量
                "quantity"=>$attrs['quantity'],
                //京东商品编号
                "skuId"=>$attrs['skuId']
            ]),
            "address"=>array(
                "provinceId"=>0,
                "cityId"=>0,
                "countyId"=>0,
                "townId"=>0,
                "fullAddress"=>$attrs['fullAddress']
            )
        );

        $ctpProtocol    =   array(
            "opName"=>$c->opName,
            "customerId"=>$c->customerId,
            "appKey"=>$c->appKey,
            "channelId"=>$c->channelId
        );

        $req->setCtpProtocol($ctpProtocol);
        $req->setStockStateParam($stockStateParam);

        $resp = $c->execute($req, $c->accessToken);

        return $resp;
    }

    /**
     * 获取兄弟sku
     * */
    public function getBrotherList($attrs   =   [])
    {
        if(empty($attrs['skuId']))
        {
            throw new InvalidArgumentException('skuId不能为空');
        }

        $c = new JdClient();

        $c->accessToken = $this->access_token();

        $req    =   new  CtpWareSkuGetBrotherListRequest();

        $protocol   =   array(
            "customerId"=>$c->customerId,
            "channelId"=>$c->channelId,
            "traceId"=>"123",
            "opName"=>$c->opName,
            "appKey"=>$c->appKey
        );

        $param   =   array(
            "skuIdSet"=>$attrs['skuId']
        );

        $req->setCtpProtocol($protocol);
        $req->setApiBrotherListParam($param);

        $resp = $c->execute($req, $c->accessToken);

        return $resp;

    }
}